'''
Data classes for the splash screens
'''

from PyQt4 import QtCore
import rgeSave
import os
from rgeSystem import *
import rgeExtra
import rgeViews

#save stuff
NAME = 0

class BaseData(object):
    def __init__(self, name = "No Name"):
        self.saved = True
        self.Filename = None
        self.unchanged = False
        self.name = name

        self.extraData = {}
        self.extraDataIndexes = {}
        self.standardData = {}
        self.standardDataIndexes = {}


    #default values


    def setName(self, NewName):
        self.name = NewName
        self.edited()

    def getName(self):
        return self.name

    def setFilename(self, NewName):
        self.Filename = NewName

    def getFilename(self):
        return self.Filename


    #data management

    def addStandard(self, Item):
        import copy
        if self.checkKeyStandard(Item.getName()):
            Item.setIndex(self.standardData[Item.getName()].getIndex())
        if Item.getIndex() < 0:
            return
        self.standardData[Item.getName()] = copy.copy(Item)
        if type(Item) not in self.standardDataIndexes:
            self.standardDataIndexes[type(Item)] = {}
        self.standardDataIndexes[type(Item)][Item.getIndex()] = Item.getName()

    def getStandard(self, Key, Type = None):
        ReturnedItem = None
        if Type:
            if Type in self.standardDataIndex:
                if Key in self.standardDataIndex[Type]:
                    Key = self.standardDataIndex[Type][Key]
                else:
                    return ReturnedItem
            else:
                return ReturnedItem
        if Key in self.standardData:
            ReturnedItem = self.standardData[Key]
            if ReturnedItem.getName() != Key:
                print "Key Error! Key: " + Key + " Real name: " + ReturnedItem.getName()
        return ReturnedItem


    def getExtra(self, Key, Type = None):
        ReturnedItem = None
        if Type:
            if Type in self.extraDataIndex:
                if Key in self.extraDataIndex[Type]:
                    Key = self.extraDataIndex[Type][Key]
                else:
                    return ReturnedItem
            else:
                return ReturnedItem
        if Key in self.extraData:
            ReturnedItem = self.extraData[Key]
            if ReturnedItem.getName() != Key:
                print "Key Error! Key: " + Key + " Real name: " + ReturnedItem.getName()
        return ReturnedItem


    def addExtra(self, Item):
        if self.checkKeyExtra(Item.getName()):
            Item.setIndex(self.extraData[Item.getName()].getIndex())
        if Item.getIndex() < 0:
            Item.setIndex(self.getFirstIndexExtra(type(Item)))
        self.extraData[Item.getName()] = Item
        if type(Item) not in self.extraDataIndex:
            self.extraDataIndex[type(Item)] = {}
        self.extraDataIndex[type(Item)][Item.getIndex()] = Item.getName()



    def checkKeyExtra(self, Key):
        return Key in self.extraData

    def checkIndexExtra(self, Index, Type):
        if Type not in self.extraDataIndexes:
            return False
        return Index in self.extraDataIndexes[Type]

    def checkKeyStandard(self, Key):
        return Key in self.standardData

    def checkIndexStandard(self, Index, Type):
        if Type not in self.standardDataIndexes:
            return False
        return Index in self.standardDataIndexes[Type]


    def getFirstIndexExtra(self, Type):
        return getFirstIndex(self.extraDataIndexes, Type)

    def getFirstIndexStandard(self, Type):
        return getFirstIndex(self.standardDataIndexes, Type)

    def getFirstIndex(self, Dict, Type):
        if Type not in Dict:
            Dict[Type] = {}
        i = 0
        while i in Dict[Type]:
            i + i +1
        return i

    #save flag

    def edited(self):
        self.saved = False

    def setSaved(self):
        self.saved = True


    #save stuff

    def saveData(self):
        if self.getFilename():
            Data = rgeSave.rgeFile()
            Data.append(QtCore.QString(self.name).toAscii(), NAME)
            for Key in self.standardData.keys():
                x = self.standardData[Key]
                if not x.getUnchanged():
                    Data.appendData(x.getFile(), rgeExtra.STANDARD_DATA)
            for Key in self.extraData.keys():
                x = self.extraData[Key]
                if not x.getUnchanged():
                    Data.appendData(x.getFile(), rgeExtra.EXTRA_DATA)
            rgeSave.saveData(Data, rgeViews.getGameFilepath() + self.getFilename())
            self.setSaved()

    def loadExtraData(self, File):
        FileItem = self.loadItem(File)
        if FileItem:
            self.addExtra(FileItem)

    def loadStandardData(self, File):
        FileItem = self.loadItem(File)
        if FileItem:
            self.addStandard(FileItem)


    def loadItem(self, File):
        FileItem = None
        FileName = "No_Name"
        FileIndex = -1
        Length = rgeSave.CharToInt(File[0:4])
        currentDataLoc = 4
        for i in range(Length):
            Data = rgeSave.pullData(File, currentDataLoc)
            TempFileItem = None
            #a switch statement would be nice here
            if Data[1] == rgeExtra.EXTRA_NAME:
                FileName = rgeSave.CharToString(Data[0])
            elif Data[1] == rgeExtra.EXTRA_INDEX:
                FileIndex = rgeSave.CharToInt(Data[0])
            elif Data[1] == rgeExtra.EXTRA_INT:
                TempFileItem = rgeExtra.IntItem()
            elif Data[1] == rgeExtra.EXTRA_FLOAT:
                TempFileItem = rgeExtra.FloatItem()
            elif Data[1] == rgeExtra.EXTRA_BOOL:
                TempFileItem = rgeExtra.BoolItem()
            elif Data[1] == rgeExtra.EXTRA_STRING:
                TempFileItem = rgeExtra.StringItem()
            elif Data[1] == rgeExtra.EXTRA_SPRITE:
                TempFileItem = rgeExtra.SpriteItem()
            elif Data[1] == rgeExtra.EXTRA_ACTOR:
                TempFileItem = rgeExtra.ActorItem()
            elif Data[1] == rgeExtra.EXTRA_SCENE:
                TempFileItem = rgeExtra.SceneItem()
            elif Data[1] == rgeExtra.EXTRA_MODULE:
                TempFileItem = rgeExtra.ModuleItem()
            elif Data[1] == rgeExtra.EXTRA_TILESET:
                TempFileItem = rgeExtra.TilesetItem()
            elif Data[1] == rgeExtra.VEC_INT_ITEM:
                TempFileItem = rgeExtra.IntVecItem()
            elif Data[1] == rgeExtra.VEC_FLOAT_ITEM:
                TempFileItem = rgeExtra.FloatVecItem()
            elif Data[1] == rgeExtra.VEC_BOOL_ITEM:
                TempFileItem = rgeExtra.BoolVecItem()
            elif Data[1] == rgeExtra.VEC_STRING_ITEM:
                TempFileItem = rgeExtra.StringVecItem()
            elif Data[1] == rgeExtra.VEC_SCENE_ITEM:
                TempFileItem = rgeExtra.SceneVecItem()
            elif Data[1] == rgeExtra.VEC_TILESET_ITEM:
                TempFileItem = rgeExtra.TilesetVecItem()
            elif Data[1] == rgeExtra.VEC_SPRITE_ITEM:
                TempFileItem = rgeExtra.SpriteVecItem()
            elif Data[1] == rgeExtra.VEC_ACTOR_ITEM:
                TempFileItem = rgeExtra.ActorVecItem()
            elif Data[1] == rgeExtra.VEC_MODULE_ITEM:
                TempFileItem = rgeExtra.ModuleVecItem()

            if TempFileItem:
                FileItem = TempFileItem
                FileItem.loadFile(Data[0])

            currentDataLoc = currentDataLoc + 4
        if FileItem and FileIndex >= 0:
            FileItem.setName(FileName)
            FileItem.setIndex(FileIndex)
            FileItem.setUnchanged(self.unchanged)
            return FileItem
        return None



    def loadFromFile(self, Filename, Unchanged = False):

        self.unchanged = Unchanged

        GameFilepath = rgeViews.getGameFilepath()

        loadedFile = rgeSave.loadData(Filename)
        if not loadedFile:
            TempFilepath = GameFilepath + Filename
            loadedFile = rgeSave.loadData(TempFilepath)
            if not loadedFile:
                print "Error loading data file. Bad file path: " + Filename
                return

        if Filename[:len(GameFilepath)] == GameFilepath:
            Filename = Filename[len(GameFilepath):]


        Length = rgeSave.CharToInt(loadedFile[0:4])
        currentDataLoc = 4

        for i in range(Length):
            Data = rgeSave.pullData(loadedFile, currentDataLoc)
            #a switch statement would be nice here
            if Data[1] == rgeExtra.STANDARD_DATA:
                self.loadStandardData(Data[0])
            elif Data[1] == rgeExtra.EXTRA_DATA:
                self.loadExtraData(Data[0])
            elif Data[1] == NAME:
                self.setName(rgeSave.CharToString(Data[0]))
            currentDataLoc = currentDataLoc + 4

        self.setFilename(Filename)